#ifndef MUSICPLAYER_H
#define MUSICPLAYER_H

#include <QWidget>
#include <QJsonArray>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QPropertyAnimation>
#include <QSqlDatabase>

#include "volumetool.h"
#include "musiclist.h"
#include "commanpage.h"
#include "lrcpage.h"


QT_BEGIN_NAMESPACE
namespace Ui { class musicPlayer; }
QT_END_NAMESPACE


class musicPlayer : public QWidget
{
    Q_OBJECT

public:
    musicPlayer(QWidget *parent = nullptr);
    ~musicPlayer();

private slots:
    void on_quit_clicked();

    void onBtFromClick(int pageid);

    void on_volume_clicked();

    void on_addLocal_clicked();

    void onUpdateLikeMusic(bool isLike , QString musicId);

    void onPlayMusic();

    void onPlayStateChanged();

    void onPlayBackModeChanged(QMediaPlaylist::PlaybackMode mode);

    //上一曲
    void onPlayUpCliked();
    //下一曲
    void onPlayDownCliked();

    void onPlayBackModeClicked();

    //播放全部
    void onPlayAll(PageType pagetype);

    //按下标播放
    void onplayMusicByIndex(CommanPage* page , int index);

    //最近播放
    void onCurrentIndexChanged(int index);

    //静音
    void setMusicSilence(bool isMuted);

    //音量控制
    void setPlayerVolume(int volumeRatio);

    //总时长控制
    void onDurationChanged(qint64 duration);

    //当前时间控制
    void onPositionChanged(qint64 position);

    //进度条控制
    void onMusicSliderChanged(float ratio);

    //歌曲名 歌手名控制
    void onMetaDataAvailableChanged(bool available);

    void on_lrcWord_clicked();

    void on_skin_clicked();

    void on_min_clicked();

    void on_max_clicked();

    void onMusicPlayerClose();

private:
    //完成初始化
    void initUI();

    void initPlayer();

    void initSQLite();

    void initMusicList();

    //将图片的路径和对应文本 随机打乱放到JsonArray
    QJsonArray RandomPicture();

    void mouseMoveEvent(QMouseEvent *event);

    void mousePressEvent(QMouseEvent *event);

    void connectSignalAndSloats();

    void playAllOfCommonPage(CommanPage* page , int index);

    void updateBtformAnimation();
private:
    Ui::musicPlayer *ui;

    QPoint drawPoint;

    VolumeTool* volumeTool;

    QSqlDatabase sqlite;

    MusicList musicList;

    //歌词界面 + 动画
    LrcPage* lrcPage;
    QPropertyAnimation* lrcPageAnimation;

    QMediaPlayer* player;
    QMediaPlaylist* playList;

    //记录当前播放音乐的界面
    CommanPage* curPage;

    //记录播放源播放音乐的总时长
    qint64 total = 0;

    //记录当前媒体源的下标
    int currentIndex = -1;

    bool isDrag = false;
};
#endif // MUSICPLAYER_H
